package arts.spbu.testswing;

import java.util.regex.*;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: pev67021
 * Date: 29.10.12
 * Time: 8:51
 */

public class RegClass {

    private Pattern pat;
    private String err;

    public RegClass(String word) {
        err = "";

        try {
            pat = Pattern.compile(word);
        }
        catch(java.util.regex.PatternSyntaxException ex) {
            err = ex.toString();
        }
    }

    public LinkedList<MatchEntry> getMatches(String text) {
        LinkedList<MatchEntry> match_list = new LinkedList<MatchEntry>();

        try {
	    Matcher mat = pat.matcher(text);
            if (!mat.find())
                return null;
            int mat_ind = mat.start();
            match_list.addLast(new MatchEntry(mat_ind, mat.end(), text));
            while (mat.find(mat_ind + 1) && mat_ind < text.length() - 1)   {
                mat_ind = mat.start();
                match_list.addLast(new MatchEntry(mat_ind, mat.end(), text));
            }
            return match_list;
        }
        catch(java.lang.NullPointerException ex)  {
            err = ex.toString() + " " + err;
            return null;
        }
    }

    public String getError(){
        return err;
    }
}


